﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace BalanceManager.Model
{
    public interface IItemsRepository
    {
        ObservableCollection<Item> LoadItems();
        void AddItem(Item item);
        void UpdateItem(Item item);
        void DeleteItem(Item item);
        bool HasChildItems(Item item);
        Item GetById(int id);
    }

    public class ItemsRepository : IItemsRepository
    {
        public ObservableCollection<Item> LoadItems()
        {
            using (BalanceManagerDataContext db = new BalanceManagerDataContext(BalanceManagerDataContext.DBConnectionString))
            {
                var items = from Item item in db.Items
                            select item;

                return new ObservableCollection<Item>(items);
            }
        }

        public void AddItem(Item item)
        {
            using (BalanceManagerDataContext db = new BalanceManagerDataContext(BalanceManagerDataContext.DBConnectionString))
            {
                db.Items.InsertOnSubmit(item);
                db.SubmitChanges();
            }
        }

        public void UpdateItem(Item item)
        {
            using (BalanceManagerDataContext db = new BalanceManagerDataContext(BalanceManagerDataContext.DBConnectionString))
            {
                var dbItem = db.Items.First(o => o.Id == item.Id);
                dbItem.Name = item.Name;
                db.SubmitChanges();
            }
        }

        public void DeleteItem(Item item)
        {
            using (BalanceManagerDataContext db = new BalanceManagerDataContext(BalanceManagerDataContext.DBConnectionString))
            {
                var dbItem = db.Items.First(o => o.Id == item.Id);
                db.Items.DeleteOnSubmit(dbItem);
                db.SubmitChanges();
            }
        }

        public bool HasChildItems(Item item)
        {
            using (BalanceManagerDataContext db = new BalanceManagerDataContext(BalanceManagerDataContext.DBConnectionString))
            {
                var child = db.Items.FirstOrDefault(i => i.ParentId == item.Id);
                return child != null;
            }
        }

        public Item GetById(int id)
        {
            using (BalanceManagerDataContext db = new BalanceManagerDataContext(BalanceManagerDataContext.DBConnectionString))
            {
                return db.Items.FirstOrDefault(o => o.Id == id);
            }
        }

        public static void InsertSampleData()
        {
            var items = new List<Item>();
            for (var i = 0; i < 15; i++)
            {
                items.Add(new Item
                {
                    Name = "Item " + i
                });
            }

            items[5].ParentId = 1;
            items[6].ParentId = 1;
            items[7].ParentId = 1;
            items[8].ParentId = 1;
            items[9].ParentId = 6;
            items[10].ParentId = 6;

            using (BalanceManagerDataContext db = new BalanceManagerDataContext(BalanceManagerDataContext.DBConnectionString))
            {
                db.Items.InsertAllOnSubmit(items);
                db.SubmitChanges();
            }
        }
    }
}
